/*************************************************************************
	> File Name: main.c
	> Author: 
	> Mail: 
	> Created Time: 2018/1/23 Tue 18:32
	> Version     :
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#include "AwinicAPI.h"
#include "AwinicLog.h"


#define  BUFFER_SIZE (2048)

#define  IN_16BITPCM_FILE "../data/16bit.pcm"
#define  IN_24BITPCM_FILE "../data/24bit.pcm"
#define  IN_32BITPCM_FILE "../data/32bit.pcm"
#define  OUT_PCM_FILE     "../data/awinic_out.pcm"
#//define  IN_TEST_FILE     "../data/test.pcm"
#define  IN_TEST_FILE     "../data/beijin_vin.pcm"

#define  IN_16BITPCMMONO_FILE "../data/16bitmono.pcm"

#define  IN_32BITPCMMONO_FILE "../data/32bitmono.pcm"
#define  OUT_PCMMONO_FILE     "../data/awinic_outmono.pcm"

static int readFileData(char* buffer,int bytes,int pos)
{
    int ret;
    FILE *fp;

    fp = fopen(IN_24BITPCM_FILE,"rb");
    if(fp == NULL)
    {
        printf("%s: open file failed \n",__func__);
        return -1;
    }

    fseek(fp,pos,SEEK_SET);
    ret = fread(buffer,sizeof(char),bytes,fp);
    if(ret <=0 )
    {
        printf("%s: ERROR:read file data failed \n",__func__);
        fclose(fp);
        return -1;
    }
    ret = ftell(fp);
    fclose(fp);
    return ret;
}

void writePcmData(char *buffer,int bytes)
{
    FILE *fp;

    fp = fopen(OUT_PCM_FILE,"ab+");
    if(fp == NULL)
    {
        printf("%s: fopen OUT_PCM_FILE failed \n",__func__);
        return;
    }

    fwrite(buffer,sizeof(char),bytes,fp);
    fclose(fp);
}


#define PARAM_BIN_FILE  "../data/awinic_params.bin"
#define PARAM_BUFFER_SIZE 1024
int readParamsAndSet(char*cfgBuf)
{
   int fp;
   int ret;
   char readParamData[PARAM_BUFFER_SIZE] = {0};
   char GetParamData[PARAM_BUFFER_SIZE] = {0};
#if HISI_PLATFORM 
   fp = open(PARAM_BIN_FILE,O_RDONLY|O_BINARY);
#else
  fp = open(PARAM_BIN_FILE,O_RDONLY);
#endif
   if(fp < 0)
   {
	   AWLOGE("open file failed \n");
       return  ADSP_EUNEXPECTED;
   }

   ret = read(fp,(char*)readParamData,PARAM_BUFFER_SIZE);
   if(ret <0)
   {
	   AWLOGE("read param data failed \n");
       close(fp);
       return ADSP_EUNEXPECTED;
   }
   //AWLOGD("read bytes is %d \n",ret);
   close(fp);

   uint16_t paramsSize = ret;
   int awinicRet;
   awinicRet=AwinicSetParams(cfgBuf,readParamData,paramsSize);
   if(awinicRet != ADSP_EOK)
   {
	   AWLOGE("set parameter failed \n");
       return ADSP_EUNEXPECTED;
   }
   awinicRet = AwinicGetParams((void*)cfgBuf,(void*)GetParamData,&paramsSize);
   if(awinicRet != ADSP_EOK)
   {
	   AWLOGE("Get parameter failed \n");
       return ADSP_EUNEXPECTED;
   }

   int cnt;
   for(cnt =0;cnt < paramsSize;cnt++)
   {
       if(readParamData[cnt] != GetParamData[cnt])
       {
    	   AWLOGE("Set parameter is different to get parameter \n");
    	   AWLOGE("i %d ,readData %d,getdata %d \n",cnt,readParamData[cnt],GetParamData[cnt]);
           return ADSP_EUNEXPECTED;
       }
   }

   return ADSP_EOK;
}

int main(int argc,char **argv)
{
    
    char dataBuf[BUFFER_SIZE*4];  //int32_t * channel_num * 2048
    media_info_t mediaInfo ;
    mediaInfo.num_channels = 2;
    mediaInfo.bits_per_sample = 24;
    mediaInfo.sampling_rate = 48000;
    mediaInfo.data_is_signed = 1;

    uint32_t awinicMemSize;

    int ret = AwinicGetSize(&awinicMemSize);
    if(ret != ADSP_EOK || awinicMemSize <=0)
    {
    	AWLOGE("awinic Get size failed \n");
        return -1;
    }

    char* awinicMemPtr = NULL;
    awinicMemPtr = (char*)calloc(awinicMemSize,sizeof(char));
    if(awinicMemPtr == NULL)
    {
    	AWLOGE(" alloc awinic memory failed \n");
        return -1;
    }

    ret = AwinicInit(awinicMemPtr,&mediaInfo);
    if(ret != ADSP_EOK)
    {
    	AWLOGE("AwinicInit failed \n");
        goto init_err;
    }

    ret = readParamsAndSet(awinicMemPtr);
    if(ret != ADSP_EOK)
    {
    	AWLOGE("awinic set parameter failed\n");
        goto init_err;
    }

    int readCnt = 1024,i,pos =0;
    enableMode_t enable = twoChEnable;
    for(i=0;i<readCnt;i++)
    {
        pos = readFileData(dataBuf,BUFFER_SIZE*4,pos);
        if(pos < 0)
        {
        	AWLOGE("read data failed %d\n",i);
            goto init_err;
        }
        AwinicHandle(awinicMemPtr,dataBuf,BUFFER_SIZE*4,enable);
        writePcmData(dataBuf,BUFFER_SIZE*4);
    }

    AwinicReset(awinicMemPtr);
    AwinicEnd(awinicMemPtr);

    free(awinicMemPtr);
    awinicMemPtr = NULL;

    return 0;

init_err:
    free(awinicMemPtr);
    awinicMemPtr = NULL;


    return -1;
}
